home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / SWar / Ships.p < prev    next >
Text File  |  1995-06-15  |  7KB  |  261 lines

  1. unit ships;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$IFC UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Events, Menus, Dialogs, Fonts, Resources, Devices,
  8. {$ENDC}
  9.         Globals, Util, Debris, SWSound;
  10.  
  11.     procedure InitShips;
  12.     procedure MoveShips;
  13.     procedure DrawShips;
  14.     function CheckForShipCollision (s: Integer; r: Rect): Boolean;
  15.     procedure KillPlayer (i: Integer);
  16.  
  17. implementation
  18.  
  19.     const
  20.         MAX_SPEED = 3;
  21.  
  22.     procedure InitShips;
  23.         var
  24.             i, j: Integer;
  25.     begin
  26.         for i := 0 to MAX_PLAYERS - 1 do
  27.             begin
  28.                 gShipRecs[i].where.v := RngRnd(25, 455);
  29.                 gShipRecs[i].where.h := RngRnd(25, 615);
  30.                 if i <> 0 then
  31.                     begin
  32.                         gShipRecs[i].vel.v := RngRnd(-MAX_SPEED, MAX_SPEED);
  33.                         gShipRecs[i].vel.h := RngRnd(-MAX_SPEED, MAX_SPEED);
  34.                     end (* if *)
  35.                 else
  36.                     begin
  37.                         gShipRecs[i].vel.v := 0;
  38.                         gShipRecs[i].vel.h := 0;
  39.                     end; (* else *)
  40.                 gShipRecs[i].dir := RngRnd(0, 7);
  41.                 gShipRecs[i].anim := RngRnd(0, 7);
  42.                 gShipRecs[i].isAlive := TRUE;
  43.                 gShipRecs[i].isAccel := FALSE;
  44.             end; (* for *)
  45.  
  46.         gShipRecs[0].color := myBlue;
  47.         gShipRecs[1].color := myRed;
  48.         gShipRecs[2].color := myYellow;
  49.         gShipRecs[3].color := myGreen;
  50.  
  51.         for i := 0 to 7 do
  52.             for j := 0 to 7 do
  53.                 begin
  54.                     SetRect(gShipSrcRects[0, i, j], 4 + 20 * j, 12 + 20 * i, 19 + 20 * j, 27 + 20 * i);
  55.                     SetRect(gShipSrcRects[1, i, j], 164 + 20 * j, 12 + 20 * i, 179 + 20 * j, 27 + 20 * i);
  56.                     SetRect(gShipSrcRects[2, i, j], 4 + 20 * j, 172 + 20 * i, 19 + 20 * j, 187 + 20 * i);
  57.                     SetRect(gShipSrcRects[3, i, j], 164 + 20 * j, 172 + 20 * i, 179 + 20 * j, 187 + 20 * i);
  58.                 end; (* for *)
  59.  
  60.     end; (* InitShips() *)
  61.  
  62.     procedure MoveShips;
  63.         var
  64.             i: Integer;
  65.     begin
  66.         for i := 0 to MAX_PLAYERS - 1 do
  67.             if (gShipRecs[i].isAlive) then
  68.                 begin
  69.                     if (gShipRecs[i].isAccel) then
  70.                         case (gShipRecs[i].dir) of
  71.                             0: 
  72.                                 gShipRecs[i].vel.v := gShipRecs[i].vel.v - 1;
  73.                             1: 
  74.                                 begin
  75.                                     gShipRecs[i].vel.v := gShipRecs[i].vel.v - 1;
  76.                                     gShipRecs[i].vel.h := gShipRecs[i].vel.h + 1;
  77.                                 end;
  78.                             2: 
  79.                                 gShipRecs[i].vel.h := gShipRecs[i].vel.h + 1;
  80.                             3: 
  81.                                 begin
  82.                                     gShipRecs[i].vel.v := gShipRecs[i].vel.v + 1;
  83.                                     gShipRecs[i].vel.h := gShipRecs[i].vel.h + 1;
  84.                                 end;
  85.                             4: 
  86.                                 gShipRecs[i].vel.v := gShipRecs[i].vel.v + 1;
  87.                             5: 
  88.                                 begin
  89.                                     gShipRecs[i].vel.v := gShipRecs[i].vel.v + 1;
  90.                                     gShipRecs[i].vel.h := gShipRecs[i].vel.h - 1;
  91.                                 end;
  92.                             6: 
  93.                                 gShipRecs[i].vel.h := gShipRecs[i].vel.h - 1;
  94.                             7: 
  95.                                 begin
  96.                                     gShipRecs[i].vel.v := gShipRecs[i].vel.v - 1;
  97.                                     gShipRecs[i].vel.h := gShipRecs[i].vel.h - 1;
  98.                                 end;
  99.                         end; (* switch *)
  100.                     if (gShipRecs[i].vel.h > MAX_SPEED) then
  101.                         gShipRecs[i].vel.h := MAX_SPEED;
  102.                     if (gShipRecs[i].vel.h < -MAX_SPEED) then
  103.                         gShipRecs[i].vel.h := -MAX_SPEED;
  104.                     if (gShipRecs[i].vel.v > MAX_SPEED) then
  105.                         gShipRecs[i].vel.v := MAX_SPEED;
  106.                     if (gShipRecs[i].vel.v < -MAX_SPEED) then
  107.                         gShipRecs[i].vel.v := -MAX_SPEED;
  108.                     gShipRecs[i].where.h := gShipRecs[i].where.h + gShipRecs[i].vel.h;
  109.                     gShipRecs[i].where.v := gShipRecs[i].where.v + gShipRecs[i].vel.v;
  110.                     if (gShipRecs[i].where.h > 624) then
  111.                         begin
  112.                             gShipRecs[i].vel.h := -gShipRecs[i].vel.h;
  113.                             gShipRecs[i].where.h := 624;
  114.                         end; (* if *)
  115.                     if (gShipRecs[i].where.h < 0) then
  116.                         begin
  117.                             gShipRecs[i].vel.h := -gShipRecs[i].vel.h;
  118.                             gShipRecs[i].where.h := 0;
  119.                         end; (* if *)
  120.                     if (gShipRecs[i].where.v > 464) then
  121.                         begin
  122.                             gShipRecs[i].vel.v := -gShipRecs[i].vel.v;
  123.                             gShipRecs[i].where.v := 464;
  124.                         end; (* if *)
  125.                     if (gShipRecs[i].where.v < 20) then
  126.                         begin
  127.                             gShipRecs[i].vel.v := -gShipRecs[i].vel.v;
  128.                             gShipRecs[i].where.v := 20;
  129.                         end; (* if *)
  130.                     if (gShipRecs[i].anim < 0) then
  131.                         gShipRecs[i].anim := 0;
  132.                     gShipRecs[i].anim := gShipRecs[i].anim + 1;
  133.                     if (gShipRecs[i].anim > 7) then
  134.                         gShipRecs[i].anim := 0;
  135.                 end; (* if *)
  136.  
  137.     end; (* MoveShips() *)
  138.  
  139.     procedure DrawShips;
  140.         var
  141.             dstRect: Rect;
  142.             i, j: Integer;
  143.     begin
  144.         for i := 0 to MAX_PLAYERS - 1 do
  145.             if (gShipRecs[i].isAlive) then
  146.                 begin
  147.                     dstRect.top := gOldShipRecs[i].where.v;
  148.                     dstRect.bottom := dstRect.top + 15;
  149.                     dstRect.left := gOldShipRecs[i].where.h;
  150.                     dstRect.right := dstRect.left + 15;
  151.                     if gOldDepth > 1 then
  152.                         begin
  153.                             RGBForeColor(myBlack);
  154.                             RGBBackColor(myBlack);
  155.                         end;
  156.                     EraseRect(dstRect);
  157.                     RGBForeColor(myBlack);
  158.                     RGBBackColor(myWhite);
  159.                     dstRect.top := gShipRecs[i].where.v;
  160.                     dstRect.bottom := dstRect.top + 15;
  161.                     dstRect.left := gShipRecs[i].where.h;
  162.                     dstRect.right := dstRect.left + 15;
  163.                     CopyBits(GrafPtr(gScrapPtr)^.portBits, GrafPtr(gOSPtr)^.portBits, gShipSrcRects[i, gShipRecs[i].dir, gShipRecs[i].anim], dstRect, srcCopy, nil);
  164.                     for j := 0 to MAX_PLAYERS - 1 do
  165.                         if (i <> j) and CheckForShipCollision(j, dstRect) then
  166.                             begin
  167.                                 KillPlayer(i);
  168.                                 KillPlayer(j);
  169.                             end; (* if *)
  170.                     gOldShipRecs[i] := gShipRecs[i];
  171.                 end; (* if *)
  172.  
  173.     end; (* DrawShips() *)
  174.  
  175.     function CheckForShipCollision (s: Integer; r: Rect): Boolean;
  176.         var
  177.             shipRect, tmpRect: Rect;
  178.     begin
  179.         CheckForShipCollision := false;
  180.         if (gShipRecs[s].isAlive) then
  181.             begin
  182.                 shipRect.top := gShipRecs[s].where.v;
  183.                 shipRect.bottom := shipRect.top + 15;
  184.                 shipRect.left := gShipRecs[s].where.h;
  185.                 shipRect.right := shipRect.left + 15;
  186.                 if (SectRect(r, shipRect, tmpRect)) then
  187.                     CheckForShipCollision := true;
  188.             end; (* if *)
  189.     end; (* CheckForShipCollision() *)
  190.  
  191.     procedure KillPlayer (i: Integer);
  192.         var
  193.             j: Integer;
  194.             dstRect: Rect;
  195.     begin
  196.         gShipRecs[i].isAlive := FALSE;
  197.  
  198.         dstRect.top := gShipRecs[i].where.v;
  199.         dstRect.bottom := dstRect.top + 15;
  200.         dstRect.left := gShipRecs[i].where.h;
  201.         dstRect.right := dstRect.left + 15;
  202.         if gOldDepth > 1 then
  203.             begin
  204.                 RGBForeColor(myBlack);
  205.                 RGBBackColor(myBlack);
  206.             end;
  207.         EraseRect(dstRect);
  208.         RGBForeColor(myBlack);
  209.         RGBBackColor(myWhite);
  210.         dstRect.top := gOldShipRecs[i].where.v;
  211.         dstRect.bottom := dstRect.top + 15;
  212.         dstRect.left := gOldShipRecs[i].where.h;
  213.         dstRect.right := dstRect.left + 15;
  214.         if gOldDepth > 1 then
  215.             begin
  216.                 RGBForeColor(myBlack);
  217.                 RGBBackColor(myBlack);
  218.             end;
  219.         EraseRect(dstRect);
  220.         RGBForeColor(myBlack);
  221.         RGBBackColor(myWhite);
  222.  
  223.         CreateDebris(5, 25, gShipRecs[i].where.h, gShipRecs[i].where.v, gShipRecs[i].vel.h, gShipRecs[i].vel.v);
  224.         if noErr <> ASndPlay(gBoomSoundH) then
  225.             ;
  226.  
  227.         if i = 0 then
  228.             begin
  229.                 gMatchIsEnding := TRUE;
  230.                 gMatchTicker := 50;
  231.             end (* if *)
  232.         else
  233.             begin
  234.                 gShipRecs[i].where.v := RngRnd(25, 455);
  235.                 gShipRecs[i].where.h := RngRnd(25, 615);
  236.                 if i <> 0 then
  237.                     begin
  238.                         gShipRecs[i].vel.v := RngRnd(-MAX_SPEED, MAX_SPEED);
  239.                         gShipRecs[i].vel.h := RngRnd(-MAX_SPEED, MAX_SPEED);
  240.                     end (* if *)
  241.                 else
  242.                     begin
  243.                         gShipRecs[i].vel.v := 0;
  244.                         gShipRecs[i].vel.h := 0;
  245.                     end; (* else *)
  246.                 gShipRecs[i].dir := RngRnd(0, 7);
  247.                 gShipRecs[i].anim := RngRnd(0, 7);
  248.                 gShipRecs[i].isAlive := TRUE;
  249.                 gShipRecs[i].isAccel := FALSE;
  250.             end; (* else *)
  251.  
  252.         for j := 1 to MAX_PLAYERS - 1 do
  253.             if gShipRecs[j].isAlive then
  254.                 exit(KillPlayer);
  255.  
  256.         gMatchIsEnding := TRUE;
  257.         gMatchTicker := 50;
  258.  
  259.     end; (* KillPlayer() *)
  260.  
  261. end.